import {Component, OnInit} from '@angular/core';
import {Router, ActivatedRoute} from "@angular/router";
import {TeamManagerService} from "../../../my-service/teamManager.service";
import {UtilService} from "../../../my-service/util.service";
import {NzMessageService} from 'ng-zorro-antd';
import {Packet} from "../../Packet";

@Component({
  selector: 'app-team-package',
  templateUrl: './team-package.component.html',
  styleUrls: ['./team-package.component.css'],
})
export class TeamPackageComponent implements OnInit {
  readImgUrl = '';
  defaultImg = '';
  navInfo = null;
  packageGuid = null; //教案包guid
  packageName = null; //教案包名字
  teamId = null; //团队guid
  teamName = null; //团队名字
  childPacketArr = [];
  currentModifyIndex: number = null;
  motherPacket: any = {};//教案包母版
  constructor(private router: Router,
              private routerInfo: ActivatedRoute,
              private teamServe: TeamManagerService,
              private message: NzMessageService,
              private utilServe: UtilService,) {
    this.readImgUrl = this.utilServe.readImgUrl;
    this.defaultImg = this.utilServe.defaultImg;
  }

  onPacketGuidGetPacket(packetGuid) {
    this.teamServe.onPacketGuidGetPacket(packetGuid).then(res => {
      if (res.code == 1) {
        res.data.forEach((packet, index) => {
          let p = new Packet(this.teamId, packet.PK_MyPacketGuid, packet.PacketName, packet.CoverUrl, packet.EnPacketName, packet.Description, packet.Publisher, packet.FK_AuthorGuid, packet.FK_CreatorGuid, packet.FK_OriginGuid);
          if (p.isDuplicate) {
            this.childPacketArr.push(p)
          } else {
            this.motherPacket = p;
          }
        });
      } else {
        this.message.error('获取教案包错误，请稍后再试');
      }
    })
  }

  ngOnInit() {
    this.packageGuid = this.routerInfo.snapshot.params.p_id;
    this.packageName = this.routerInfo.snapshot.params.p_name;
    this.teamId = this.routerInfo.snapshot.params.id;
    this.teamName = this.routerInfo.snapshot.params.name;
    this.onPacketGuidGetPacket(this.packageGuid);
    this.navInfo = {
      p_name: this.packageName,
      id: this.teamId,
      teamName: this.teamName
    };
  }

  /*创建教案包副本*/
  createDuplicate(teamGuid, packetGuid, packetName) {
    this.utilServe.comfirm(`您确认创建${packetName}教案包副本吗？`, () => {
      this.teamServe.createDuplicate(teamGuid, packetGuid).then(res => {
        if (res.code == 1) {
          let packet = res.data;
          this.childPacketArr.unshift(new Packet(teamGuid, packet.PK_MyPacketGuid, packet.PacketName, packet.CoverUrl, packet.EnPacketName, packet.Description, packet.Publisher, packet.FK_AuthorGuid, packet.FK_CreatorGuid, packet.FK_OriginGuid));
          this.message.success(`您成功创建${packetName}教案包副本`);
        } else {
          this.message.error(`您创建${packetName}教案包副本失败，请稍后再试`);
          console.log(res);
        }
      })
    })
  }

  /*删除教案包*/
  deleteDuplicate(packetGuid, packetName, index) {
    this.utilServe.comfirm(`您确认删除${packetName}吗？`, () => {
      this.teamServe.deleteDuplicate(packetGuid).then(res => {
        if (res.code == 1) {
          this.childPacketArr.splice(index, 1);
          this.message.success(`您成功删除${packetName}`);
        } else {
          this.message.error(`您删除${packetName}失败，请稍后再试`);
          console.log(res);
        }
      })
    })
  }

  /*打开修改教案包*/
  modifyDuplicate(duplicate, index) {
    duplicate.isModify = true;
    this.currentModifyIndex = index;
  }

  /*关闭修改教案包*/
  closeModifyDuplicate(event) {
    this.childPacketArr[this.currentModifyIndex] = event;
  }

  //去教案包应用
  goPacket(packetGuid, packetName, flag) {
    if (flag) {
      return;
    } else {
      this.message.warning(`请您从超级教案中的团队教案包进入${packetName}`);
    }
  }
}
